var bodyParser = require('body-parser');

var sql = require('../sql');

var addNotice = function(req, res) {
    var type = req.body.type;
    var fromPersonId = req.body.fromPersonId;
    var toPersonId = req.body.toPersonId;
    var content = req.body.content;
    var projectid = req.body.projectid;
    var taskid = req.body.taskid;
    sql.Notice.create({
        'status': 1,
        'type': type,
        'fromPersonId': fromPersonId,
        'toPersonId': toPersonId,
        'content': content,
        'projectid': projectid,
        'taskid': taskid
    }).then(function(notice) {
        res.json(notice)
    }).catch(function(err) {
        console.error(err)
    })
};
var updateNotice = function(req, res) {
    var id = req.body.id;
    var status = req.body.status;
    sql.Notice.update({
        'status': status,
    }, {
        'where': {
            'id': id
        }
    }).then(function(notice) {
        res.json(notice)
    }).catch(function(err) {
        console.error(err)
    })
};

var getNotices = function(req, res) {
    var toPersonId = req.body.toPersonId;
    var sqlString = 'select n.*,t.taskcomments,p.projectname from notices n left join tasks t on (n.taskid = t.id) left join projects p on (n.projectid = p.id) where n.toPersonId = ' + toPersonId + ' and n.status = 1';
    sql.sequelize.query(sqlString).spread(function(results, metadata) {
        res.json(results)
    });
    // sql.Notice.findAll({
    //     'where': {
    //         'toPersonId': toPersonId
    //     }
    // }).then(function(notices) {
    //     res.json(notices)
    // }).catch(function(err) {
    //     console.error(err)
    // })
}


module.exports = { addNotice, updateNotice, getNotices }